package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telecom.Call;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 添加支付宝账号
 *
 */


public class AddALIAccount extends BaseActivity implements View.OnClickListener{
    private String TAG ="addAmount";
    private ImageView back;
    private EditText inacount;
    private EditText inname;
    private CheckBox type;
    private Button surebt;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String name;
    private String account;
    private String state ="0";
    private String from;
    private String id;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_aliaccount);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid","1");
        userId = sharedPreferences.getString("userId", "0");
        token = sharedPreferences.getString("token", "2");
        initAddAliView();
        Intent intent = getIntent();
        from = intent.getStringExtra("from");
        if (from.equals("bianji")){
            id = intent.getStringExtra("id");
            getAccountMessage(id);
        }else {
            getAccountMessage(id);
        }
    }

        // 获取账户信息
    private void getAccountMessage(String id) {
        RequestParams params=  new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETOUTACOUNTMSG);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();
        object.put("cashAccountId",id);
        params.addBodyParameter("params",object.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d(TAG, "onSuccess: 获取账户信息--"+result);
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");
                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);

                String id1 = obj1.getString("id");
                String isDefault = obj1.getString("isDefault");
                String payeeAccount = obj1.getString("payeeAccount");
                String realName = obj1.getString("realName");
                inname.setText(realName);
                inacount.setText(payeeAccount);
                if (isDefault.equals("0")){
                    type.setChecked(false);
                }else {
                    type.setChecked(true);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d(TAG, "onError: 获取账户信息--"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initAddAliView() {
        back = (ImageView) findViewById(R.id.mine_addAli_back);
        inacount = (EditText) findViewById(R.id.addali_inaccount);
        inname = (EditText) findViewById(R.id.addali_inname);
        type = (CheckBox) findViewById(R.id.addali_type);
        surebt = (Button) findViewById(R.id.addali_surebt);
        backmain = (ImageView) findViewById(R.id.addali_backmain);

        backmain.setOnClickListener(this);
        back.setOnClickListener(this);
        type.setOnClickListener(this);
        surebt.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.addali_backmain:
                startActivity(new Intent(AddALIAccount.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.mine_addAli_back:
                finish();
                break;
            case R.id.addali_surebt: //提交
                getInData();
                if (from.equals("bianji")){
                    modifyAccount(id);
                }else {
                    if (name.equals("")){
                        Toast.makeText(this, "请填写支付宝开户姓名", Toast.LENGTH_SHORT).show();
                    }else {
                        if (account.equals("")){
                            Toast.makeText(this, "请填写支付宝账户", Toast.LENGTH_SHORT).show();
                        }else {
                            SendAcountMessage();
                        }
                    }
                }

                break;
            case R.id.addali_type:// 是否设置默认
                if (type.isChecked()){
                    state="1";
                }else {
                    state="0";
                }
                break;
        }
    }
    //编辑账户
    private void modifyAccount(String id) {
        getInData();
        RequestParams params=  new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.MODIFYOUTACCOUNT);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();
        object.put("cashAccountId",id);
        object.put("realName",name);
        object.put("payeeAccount",account);
        object.put("isDefault",state);
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d(TAG, "onSuccess: 修改账户信息--"+result);
                Toast.makeText(AddALIAccount.this, "账户修改成功", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d(TAG, "onError: 修改账户信息--"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //添加账户
    private void SendAcountMessage() {
        getInData();
        RequestParams params=  new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.ADDOUTACCOUNT);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();
        object.put("realName",name);
        object.put("payeeAccount",account);
        object.put("isDefault",state);
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d(TAG, "onSuccess: 添加账户信息--"+result);
                Toast.makeText(AddALIAccount.this, "添加账户成功", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d(TAG, "onError: 添加账户信息--"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
        // 获取输入信息
    private void getInData() {
        name = String.valueOf(inname.getText());
        account = String.valueOf(inacount.getText());


    }
}
